//
//  DownloadCell.m
//  ZXDownload
//
//  Created by xin on 2018/5/14.
//  Copyright © 2018年 魏鑫. All rights reserved.
//

#import "DownloadCell.h"
#import "ZXDownloadManager.h"
#import "MCDownloadManager.h"

@implementation DownloadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setUrl:(NSString *)url
{
    _url = url;
    
    //更新UI
//    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:url];
//    self.namelLabel.text = receipt.truename;
//    self.speedLabel.text = nil;
//    self.bytesLabel.text = nil;
//    self.progressView.progress = 0;
//    self.progressView.progress = receipt.progress.fractionCompleted;
//
//    if (receipt.state == MCDownloadStateDownloading) {
//        [self.downloadButton setTitle:@"停止" forState:UIControlStateNormal];
//    }else if (receipt.state == MCDownloadStateCompleted) {
//        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
//    }else {
//        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
//    }
//
//    receipt.successBlock = ^(NSURLRequest * _Nullable r, NSHTTPURLResponse * _Nullable p, NSURL * _Nonnull url) {
//        NSLog(@"cell 下载完成通知");
//        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
//    };
//
//    receipt.failureBlock = ^(NSURLRequest * _Nullable r, NSHTTPURLResponse * _Nullable p, NSError * _Nonnull error) {
//        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
//    };
    
    
    ZXDownloadFile *file = [[ZXDownloadManager sharedManager] getDownloadFileForURL:url];
    
    self.namelLabel.text = self.url.lastPathComponent;
    self.speedLabel.text = nil;
    self.bytesLabel.text = nil;
    self.progressView.progress = 0;
    self.progressView.progress = file.progress.fractionCompleted;
    
    if (file.state == ZXDownloadFileStateDownloading) {
        [self.downloadButton setTitle:@"停止" forState:UIControlStateNormal];
    }else if (file.state == ZXDownloadFileStateCompleted) {
        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
    }else {
        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
    }
    
    file.zx_progressBlock = ^(NSProgress * _Nonnull progress, ZXDownloadFile *file) {
        
    };
    
    file.zx_successBlock = ^(NSURL * _Nonnull url) {
        NSLog(@"cell 下载完成通知");
        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
    };
    
    file.zx_failureBlock = ^(NSError * _Nonnull error) {
        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
    };
}

- (IBAction)downloadButtonClick:(UIButton *)sender {
    
//    MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:self.url];
//
//    if (receipt.state == MCDownloadStateDownloading) {
//        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
//    }else if (receipt.state == MCDownloadStateCompleted) {
//
//    }else {
//        [self.downloadButton setTitle:@"停止" forState:UIControlStateNormal];
//        [self download];
//    }
    
    ZXDownloadFile *file = [[ZXDownloadManager sharedManager] getDownloadFileForURL:self.url];
    
    if (file.state == ZXDownloadFileStateDownloading) {
        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
    }else if (file.state == ZXDownloadFileStateCompleted) {
        
    }else {
        [self.downloadButton setTitle:@"停止" forState:UIControlStateNormal];
        [self download];
    }
}

- (void)download {
//    [[MCDownloadManager defaultInstance] downloadFileWithURL:self.url progress:^(NSProgress * _Nonnull downloadProgress, MCDownloadReceipt * _Nonnull receipt) {
//        self.progressView.progress = downloadProgress.fractionCompleted;
//        self.bytesLabel.text = [NSString stringWithFormat:@"%0.2fm/%0.2fm",downloadProgress.completedUnitCount/1024.0/1024,downloadProgress.totalUnitCount/1024.0/1024];
//        self.speedLabel.text = [NSString stringWithFormat:@"%@/s",receipt.speed];
//    } destination:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSURL * _Nonnull filePath) {
//        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
//    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
//        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
//    }];
    
    [[ZXDownloadManager sharedManager] downloadFileWithURL:self.url progress:^(NSProgress * _Nonnull progress, ZXDownloadFile *file) {
        self.progressView.progress = progress.fractionCompleted;
        self.bytesLabel.text = [NSString stringWithFormat:@"%0.2fm/%0.2fm",progress.completedUnitCount/1024.0/1024,progress.totalUnitCount/1024.0/1024];
        self.speedLabel.text = [NSString stringWithFormat:@"%@/s",file.speed];
    } success:^(NSURL * _Nonnull url) {
        [self.downloadButton setTitle:@"完成" forState:UIControlStateNormal];
    } failure:^(NSError * _Nonnull error) {
        [self.downloadButton setTitle:@"下载" forState:UIControlStateNormal];
    }];
    
}

@end
